package com.bhs.sansonglogistics.ui.mine;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.BillDataBean;
import com.bhs.sansonglogistics.bean.OrderGoodsDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.BillAdapter;
import com.bhs.sansonglogistics.ui.adapter.OrderGoodsAdapter;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 在途资金
 */
public class CashTransitActivity extends BaseActivity implements NetCallBack {

    private ClipData mClipData;   //剪切板Data对象
    private ClipboardManager mClipboardManager;   //剪切板管理工具类
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private OrderGoodsAdapter mAdapter;
    private int page = 1;
    private TextView mTvUnliquidated;

    @Override
    public int initLayout() {
        return R.layout.activity_cash_transit;
    }

    @Override
    public void initView() {
        // 1. 注册mClipboardManager
        mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("在途资金");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
        mTvUnliquidated = findViewById(R.id.tv_Unliquidated);
        mTvUnliquidated.setText(String.format("未结算收入：￥%s",getIntent().getStringExtra("unliquidated")));
    }

    @Override
    public void initData() {
        mAdapter = new OrderGoodsAdapter(6);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        loadData();
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mClipData = ClipData.newPlainText("Simple text", mAdapter.getData().get(position).getDeliver_sn());
                //把clip对象放在剪贴板中
                mClipboardManager.setPrimaryClip(mClipData);
                ToastUtil.show("复制成功");

            }
        });
    }

    private void loadData() {
        networkRequest(netApi.onway_money_list(page, 10), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        OrderGoodsDataBean bean = new Gson().fromJson(json, OrderGoodsDataBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}